package com.genghis.ptas.person;

import com.genghis.ptas.classify.entity.Type;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.course.entity.CourseInfo;
import com.genghis.ptas.course.service.CourseService;
import com.genghis.ptas.note.entity.Note;
import com.genghis.ptas.note.service.NoteService;
import com.genghis.ptas.security.entity.Role;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.entity.UserBaseInfo;
import com.genghis.ptas.security.service.RoleService;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.statistic.entity.TypeStatistic;
import com.genghis.ptas.statistic.service.StatisticService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xml.sax.SAXException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("/person/")
public class PersonController {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private NoteService noteService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private StatisticService statisticService;

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "myInfo", method = {RequestMethod.GET})
    public String findPersonInfo(HttpServletRequest request) {
        return returnMyInfo(request);
    }

    private String returnMyInfo(HttpServletRequest request) {
        String loginName = LoginUserUtil.findLoginUserName();
        User user = userService.findFullUserByUserLoginName(loginName);
        Role role = roleService.findRoleByUserId(user.getId());
        request.setAttribute("notAdmin", !DictConstant.ROLE_SYSTEM.equals(role.getRoleName()));
        request.setAttribute("user", user);
        return "frontstage/person/personInfo";
    }

    @RequestMapping(value = "editUserInfo", method = {RequestMethod.POST})
    @ResponseBody
    public void editUserInfo(User user, UserBaseInfo userBaseInfo) {
        user.setId(userBaseInfo.getUserId());
        userService.editUser(user);
        userService.editUserBaseInfo(userBaseInfo);
    }

    @RequestMapping(value = "myNoteDo", method = {RequestMethod.GET})
    public String myNoteDo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<TypeValues> typeValuesList = classifyService.findAllTypeValueList();
        List<Type> typeList = classifyService.findAllType();
        List<Note> notes = noteService.findNoteByType(new Note("", "", 0, user.getId()));
        request.setAttribute("notes", notes);
        request.setAttribute("user", user);
        request.setAttribute("typeValueses", typeValuesList);
        request.setAttribute("typeList", typeList);
        return "frontstage/person/myNoteDo";
    }

    @RequestMapping(value = "upPhoto", method = {RequestMethod.GET, RequestMethod.POST})
    public String upPhoto(HttpServletRequest request) throws ServletException, IOException, ParserConfigurationException, SAXException, XPathExpressionException {
        userService.upPhoto(request, "upPhoto");
        return returnMyInfo(request);
    }

    @RequestMapping(value = "userStatisticInfoDo", method = {RequestMethod.GET})
    public String userStatisticInfoDo(HttpServletRequest request) {
        return "frontstage/person/userStatisticInfoDo";
    }

    @RequestMapping(value = "userStatisticInfo", method = {RequestMethod.POST})
    @ResponseBody
    public List<TypeStatistic> userStatisticInfo() {
        String userName = LoginUserUtil.findLoginUserName();
        User user = userService.findFullUserByUserLoginName(userName);
        return statisticService.analysisUserLike("" + user.getId());
    }

    @RequestMapping(value = "topCollectCourse", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> topCollectCourse() {
        String userName = LoginUserUtil.findLoginUserName();
        User user = userService.findFullUserByUserLoginName(userName);
        return courseService.findTopCollectCourse(user.getId());
    }

    @RequestMapping(value = "topRegisterCourse", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> topRegisterCourse() {
        String userName = LoginUserUtil.findLoginUserName();
        User user = userService.findFullUserByUserLoginName(userName);
        return courseService.findTopRegisterCourse(user.getId());
    }

    @RequestMapping(value = "myCenter", method = {RequestMethod.GET})
    public String myCenter(HttpServletRequest request) {
        request.setAttribute("showNo", request.getParameter("showNo"));
        return "frontstage/person/myCenter";
    }

    @RequestMapping(value = "myCenterDo", method = {RequestMethod.GET})
    public String myCenterDo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        int countStudy = courseService.findRegisterThisMonth(user);
        String countStudyTime = courseService.sumStudyTimeThisMonth(user);
        countStudyTime="".equals(countStudyTime)||null==countStudyTime ? "0" : countStudyTime;
        request.setAttribute("user", user);
        request.setAttribute("countStudy", countStudy);
        request.setAttribute("countStudyTime", countStudyTime);
        return "frontstage/person/myCenterDo";
    }

}
